package thread;

import java.util.Random;

/**
 * Creater with IntelliJ IDEA.
 * Description:
 * User: 86152
 * Dae: 2022-07-21
 * Time: 13:06
 */
class RuntimeData {
    long sumji=0;
    long sumou=0;
    long sum=0;
}
public class demo3 {

    //多线程
    public static void main(String[] args) {
        int[] arr = new int[100000000];
        int len=arr.length;
        RuntimeData runtimeData = new RuntimeData();
        //生成随机数
        for(int i=0;i< len;i++) {
            Random random = new Random();
            arr[i]=random.nextInt(101);
        }
        Thread t1=new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=1;i< len;i+=2) {
                    runtimeData.sumji+=arr[i];
              }
                System.out.println("奇数："+runtimeData.sumji);
            }
        });
        Thread t2=new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i< len;i+=2) {
                    runtimeData.sumou+=arr[i];
                }
                System.out.println("偶数："+runtimeData.sumou);
            }
        });
        long begin=System.currentTimeMillis();
        t1.start();
        t2.start();
        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        runtimeData.sum=runtimeData.sumji+runtimeData.sumou;
        System.out.println("总和："+runtimeData.sum);
        long end=System.currentTimeMillis();

        System.out.println("用时："+(end-begin)+"ms");
    }
    //单线程
//    public static void main(String[] args) {
//        int[] arr = new int[100000000];
//        int len=arr.length;
//        //生成随机数
//        for(int i=0;i< len;i++) {
//            Random random = new Random();
//            arr[i]=random.nextInt(101);
//        }
//        //对奇数求和
//        long begin=System.currentTimeMillis();
//        long sumji=0;
//        for(int i=0;i< len;i++) {
//            if(arr[i]%2!=0) {
//                sumji+=arr[i];
//            }
//        }
//        long sumou=0;
//        for(int i=0;i< len;i++) {
//            if(arr[i]%2==0) {
//                sumou+=arr[i];
//            }
//        }
//        long end=System.currentTimeMillis();
//        System.out.println("奇数："+sumji+" 偶数："+sumou);
//        System.out.println("用时："+(end-begin)+"ms");
//    }
}
